package com.me.forum.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.me.enums.RespType;
import com.me.forum.domain.Image;
import com.me.forum.request.ImageRequest;
import com.me.forum.service.impl.ImageService;
import com.me.forum.utils.components.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/image")
public class AdminImageController {

    @Autowired
    private ImageService imageService;

    //通过id查询图片
    @GetMapping("/{id}")
    public IModel getById(@PathVariable String id, IModel model){
        Image image = imageService.findById(id);
        model.put("success", image != null);
        model.put("category", image);
        return model;
    }

    //获取所有图片、
    @GetMapping
    public IModel getAllImage(@RequestBody ImageRequest ireq, IModel model, String page, String size){
        IPage<Image> images = imageService.getImage(ireq,page,size);
        model.success(images!= null && images.getSize() != 0);
        model.add(RespType.Images,images);
        return model;
    }

    //添加图片
    @PostMapping
    public IModel ImageAdd(@RequestBody ImageRequest ireq, IModel model) {
        Image image = imageService.addImage(ireq);
        model.success(image != null);
        return model;
    }

    //修改图片
    @PutMapping
    public IModel ImageUpdate(@RequestBody ImageRequest ireq,IModel model) {
        model.success(imageService.updateByid(ireq));
        return model;
    }

    //删除图片
    @DeleteMapping
    private IModel ImageDelete(@PathVariable String id,IModel model) {
        model.success(imageService.delete(id));
        return model;
    }
}
